package com.future.lock.me.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.future.baselib.activity.BaseActivity;
import com.future.baselib.entity.MessageEvent;
import com.future.baselib.utils.EventBusUtil;
import com.future.baselib.utils.PatternUtils;
import com.future.baselib.utils.UserInfoSharePreference;
import com.future.lock.R;
import com.future.lock.common.Constants;
import com.future.lock.common.MyApp;
import com.future.lock.common.activity.MainActivity;
import com.future.lock.common.http.HttpUtils;
import com.future.lock.common.http.response.DefaultHttpResponse;
import com.future.lock.me.entity.response.LoginResponse;
import com.igexin.sdk.PushManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class BindingPhoneActivity extends BaseActivity {

    public static final String WECHAT_TOKEN = "wx_token";
    public static final String WECHAT_ICON = "wx_icon";
    public static final String WECHAT_NAME = "wx_name";

    @BindView(R.id.view_line)
    View viewLine;
    @BindView(R.id.et_mobile)
    EditText etMobile;
    @BindView(R.id.btn_getCode)
    Button btnGetCode;
    @BindView(R.id.et_code)
    EditText etCode;

    String wxToken;
    String wxIcon;
    String wxName;

    @Override
    protected void initContentView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_binding_phone);
        ButterKnife.bind(this);
    }

    @Override
    protected void initView() {
        viewLine.setVisibility(View.GONE);
    }

    @Override
    protected void initLogic() {
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        wxToken = extras.getString(WECHAT_TOKEN);
        wxIcon = extras.getString(WECHAT_ICON);
        wxName = extras.getString(WECHAT_NAME);
    }

    @OnClick({R.id.btn_getCode, R.id.tv_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_getCode:
                getCode();
                break;
            case R.id.tv_login:
                bindingPhone();
                break;
        }
    }

    private void bindingPhone() {
        final String mobile = etMobile.getText().toString().trim();
        if (!PatternUtils.mobilePattern(toast,mobile)){
            return;
        }
        String code = etCode.getText().toString().trim();
        if (TextUtils.isEmpty(code)) {
            toast("验证码不能为空");
            return;
        }
        if (code.length()<4) {
            toast("请正确输入验证码");
            return;
        }

        HttpUtils.getHttpApi()
                .login(mobile,code, PushManager.getInstance().getClientid(this),wxToken,wxIcon,wxName,"1").enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                LoginResponse loginResponse = new LoginResponse();
                loginResponse.parse(response.body());
                toast(loginResponse.msg);
                if (loginResponse.success == 1) {//请求成功
                    UserInfoSharePreference.getInstance(BindingPhoneActivity.this).setUserToken(loginResponse.loginBean.user_token);
                    MyApp.setUserToken(loginResponse.loginBean.user_token);
                    MyApp.setUserInfo(loginResponse.loginBean);
                    EventBusUtil.sendEvent(new MessageEvent(Constants.REFRESH_LOCK_DATA));
                    startActivity(MainActivity.class);
                    finish();
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                toast(t.getMessage());
            }
        });
    }

    @SuppressWarnings("unchecked")
    private void getCode() {
        String mobile = etMobile.getText().toString().trim();
        if (!PatternUtils.mobilePattern(toast, mobile)) {
            return;
        }
        btnGetCode.setClickable(false);
        HttpUtils.getHttpApi().getCode(mobile).enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                new Thread(runnable).start();
                DefaultHttpResponse defaultResponse = new DefaultHttpResponse();
                defaultResponse.parse(response.body());
                toast(defaultResponse.msg);
                if (defaultResponse.success == 1) {//请求成功
                    etCode.setText(defaultResponse.data);
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                toast(t.getMessage());
            }
        });
    }


    //验证码发送倒计时
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            runOnUiThread(() -> btnGetCode.setClickable(false));
            for (int i = 60; i > 0; i--) {
                final int finalI = i;
                runOnUiThread(() -> btnGetCode.setText("重新获取(" + finalI + ")"));
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            runOnUiThread(() -> {
                btnGetCode.setClickable(true);
                btnGetCode.setText("重新获取");
            });
        }
    };
}
